﻿//Author:Christian TQ
//License:MIT License

$(document).ready(function () {
    var cls = { a: ".mb-sendTo", b: ".mb-urlLink", c: ".mb-imgLink" };
    for (e in cls)
        clearText(cls[e]);

    $('.mb-divImage').click(function () {
        showImgDialog($(this).html().replace("class=mb-Image", ""));
    });
});


function showImgDialog(img) {
    var _html = document.createElement('div');
    _html.innerHTML = img;
    var _options = { html: _html };
    SP.UI.ModalDialog.showModalDialog(_options);
}

function ShowImage(obj) 
{
    alert($(obj).next().html());
}

function toggleSendTo()
{
    mbToggle('mb-sendTo');
}

function toggleUrlLink() {
    mbToggle('mb-urlLink');
}

function toggleImgLink() {
    mbToggle('mb-imgLink');
}


function mbToggle(cls) 
{
    clearText(cls);
    $('.'+cls).slideToggle();
}

function clearText(cls) {
    if($('.' + cls + ' input').val('').length>0)
        $('.' + cls + ' input').val('');
}
var totalPages;
function AddPager() {
    var pageItems = config.pageItems;
    var number_of_items = $('#microBlogContent').children().size();
    totalPages = Math.ceil(number_of_items / pageItems);
    $('#curPage').val(0);
    $('#pageItems').val(pageItems);
    var pagerHtml = '<div style="position:absolute;left:190px;"><div id="resultPager" style="display:none;"><a class="prevLink" href="javascript:navPrev();"><img alt="Previous" src="/_layouts/1033/images/prev.gif" border="0"/></a>';
    pagerHtml += '<a id="pageNum">1</a>' + '<a style="text-decoration:none;"> of ' + totalPages + '</a>';
    pagerHtml += '<a class="nextLink" href="javascript:navNext();"><img alt="Next" src="/_layouts/1033/images/next.gif" border="0"/></a><div></div>';
    $('#pageNav').html(pagerHtml);
    $('#microBlogContent').children().css('display', 'none');
    $('#microBlogContent').children().slice(0, pageItems).css('display', 'block');
    $('#resultPager').attr('style', 'display:block');
}

function navPrev() {
    currentPage = parseInt($('#curPage').val()) - 1;
    if (currentPage > -1)
        go_to_page(currentPage);
}

function navNext() {
    currentPage = parseInt($('#curPage').val()) + 1;
    if (currentPage < totalPages)
        go_to_page(currentPage);

}

function go_to_page(page_num) {
    $('#pageNum').text(page_num + 1);
    var pageItems = parseInt($('#pageItems').val());
    start_from = page_num * pageItems;
    end_on = start_from + pageItems;
    $('#microBlogContent').children().css('display', 'none').slice(start_from, end_on).css('display', 'block');
    $('#curPage').val(page_num);
}  